package com.dhsc.service.impl.jyjk.zxyc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhsc.domain.jkzx.zxjh.GlyfzVo;
import com.dhsc.domain.jkzx.zxjh.YcjkyzVo;
import com.dhsc.domain.jkzx.zxjh.YzszVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.service.jyjk.zxyc.IYzszService;
import com.dhsc.util.CommonUtil;
import com.dhsc.view.dao.jyjk.zxyc.IyzszDao;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class YzszServiceImpl implements IYzszService {

    @Autowired
    private IyzszDao yzszDao;

    @Override
    public LayuiTableVo selectzruleinfo(YzszVo y) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(y.getPage(), y.getLimit());
            List<YzszVo> list = yzszDao.selectzruleinfo(y);
            PageInfo<YzszVo> pageInfo = new PageInfo<>(list);
            obj.setCount(pageInfo.getTotal());
            obj.setData(list);
            obj.setMsg("");
            obj.setCode(0);
        }catch(Exception e){
            log.error("查查询账户实名制异常监控规则表信息出错", e);
            throw new MyException("查查询账户实名制异常监控规则表信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject selectFZDictionaryByParent(YzszVo y) {
        ResponseObject obj = new ResponseObject();
        try{
            List<YzszVo> list = yzszDao.selectFZDictionaryByParent(y);
            obj.setData(list);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("查询阀值字典表通过父类查询子类出错", e);
            throw new MyException("查询阀值字典表通过父类查询子类出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject selectZThresholdSet(GlyfzVo g) {
        ResponseObject obj = new ResponseObject();
        try{
            List<GlyfzVo> list = yzszDao.selectZThresholdSet(g);
            obj.setData(list);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("查询管理员阀值表出错", e);
            throw new MyException("查询管理员阀值表出错",e);
        }
        return obj;
    }

    @Transactional("viewTransactionManager")
    @Override
    public ResponseObject setZThresholdSet(GlyfzVo g) {
        ResponseObject obj = new ResponseObject();
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            String[] arrjs = objectMapper.readValue(g.getArrjs(), String[].class);
            String[] arryj = objectMapper.readValue(g.getArryj(), String[].class);
            String[] arrgfx = objectMapper.readValue(g.getArrgfx(), String[].class);
            g.setArrjs(StringUtils.join(arrjs, ",")==null?"":StringUtils.join(arrjs, ","));
            g.setArrgfx(StringUtils.join(arrgfx, ",")==null?"":StringUtils.join(arrgfx, ","));
            g.setArryj( StringUtils.join(arryj, ",")==null?"":StringUtils.join(arryj, ","));
            yzszDao.delZThresholdSet(g);
            yzszDao.addZThresholdSet(g);
            settingZThreshold(g.getName_code(),arrjs,1,g.getUserId());
            settingZThreshold(g.getName_code(),arryj,2,g.getUserId());
            //高风险
            settingZThreshold(g.getName_code(),arrgfx,3,g.getUserId());
            obj.setStatus(200);
        }catch(Exception e){
            log.error("管理员设置阀值表出错", e);
            throw new MyException("管理员设置阀值表出错",e);
        }
        return obj;
    }

    public void settingZThreshold(String name, String []arrjs, int type,int userId)
    {
        Map<String,Object> map = new HashMap<>();
        map.put("name", name);
        map.put("type", type);
        List<Map<String,Object>> lists = yzszDao.selectzthresholdByChildrenCode(map);

        int id = yzszDao.selectZRuleIdByName(map);

        List<String> list1 =new ArrayList<>();
        List<String> list3 =new ArrayList<>();
        if(CommonUtil.isNotEmptyObj(arrjs))
        {
            for(String arr : arrjs)
            {
                list1.add(arr);
                list3.add(arr);
            }
        }
        List<String> list2 =new ArrayList<>();
        if(CommonUtil.isNotEmptyList(lists))
        {
            for(int i=0;i<lists.size();i++)
            {
                list2.add(lists.get(i).get("NAME").toString());
            }
        }
        List<String> insertlist = romovelist(list1,list2);
        if(CommonUtil.isNotEmptyList(insertlist))
        {
            for(int i=0;i<insertlist.size();i++)
            {
                YcjkyzVo ycjkyzVo = new YcjkyzVo();
                ycjkyzVo.setRule_id(String.valueOf(id));
                ycjkyzVo.setName(insertlist.get(i));
                ycjkyzVo.setLowerlimit("无");
                ycjkyzVo.setToplimit("无");
                ycjkyzVo.setType(type);
                ycjkyzVo.setState(1);
                ycjkyzVo.setSet_people(String.valueOf(userId));
                yzszDao.addZthreshold(ycjkyzVo);
            }
        }
        List<String> dellist = romovelist(list2,list3);
        if(CommonUtil.isNotEmptyList(dellist))
        {
            for(int i=0;i<dellist.size();i++)
            {
                YcjkyzVo ycjkyzVo = new YcjkyzVo();
                ycjkyzVo.setRule_id(String.valueOf(id));
                ycjkyzVo.setName(dellist.get(i));
                ycjkyzVo.setType(type);
                ycjkyzVo.setState(2);
                ycjkyzVo.setSet_people(String.valueOf(userId));
                yzszDao.delZthreshold(ycjkyzVo);
            }
        }
    }
    public List<String> romovelist(List<String> lista,List<String> listb)
    {
        lista.removeAll(listb);
        return lista;
    }
}
